var app = getApp();
var phone;
var acorres=[];
let jumpurl = app.jumpurl.url;
let tag;
let page=1;
let currentTab;
Page({
  data: {
    winHeight: "",//窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    modalHidden2: true,
    noneData: false,//得到是否显示没有更多数据,
    totalPage: 0,//得到总的页数
    
    phone:[{
      number:'15152186227'
    }]
  },
  // 滚动切换标签样式
  switchTab: function (e) {
    var that=this;
    tag = e.detail.current;
    currentTab = e.detail.current;
    console.log("**************"+currentTab+"****************");
    that.setData({
      currentTab: e.detail.current,
       noneData: false
    });
   wx.request({
      url: jumpurl + 'dingdan/index',
      data: {
        tag: e.detail.current,
        page: 1,
        openid: app.userList.openid
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //post
      },
      success: function (res) {
        console.log(res);
        var flag=res.data.msg.SUCCESS;
        if(flag=="F"){
          console.log("************ffff************");
          that.setData({
            isnone:true,
            noneData:false,
            acorres: res.data.msg.message
          })
        }else{
          var data_len = res.data.msg.message.length;
            acorres = res.data.msg.message
            that.setData({
              noneData: false,
              isnone:false,
              acorres: res.data.msg.message
            })
            page = page + 1;

         
        }
        
      },
      fail: function (err) {
        console.log(err);
      }
   }),
     this.checkCor();
   
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    var that = this;
    
    if (that.data.currentTab == cur) { return false; }
    else {
      this.setData({
        currentTab: cur
      })
    }
    wx.request({
      url: jumpurl + 'dingdan/index',
      data: {
        tag: cur,
        page: 1,
        openid: app.userList.openid
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //post
      },
      success: function (res) {
      
        var flag = res.data.msg.SUCCESS;
      
        if (flag == "F") {
          that.setData({
            isnone: true,
            noneData: false,
            acorres: res.data.msg.message
          })
        } else {
          var data_len = res.data.msg.message.length;
          acorres = res.data.msg.message
          that.setData({
            noneData: false,
            isnone: false,
            acorres: res.data.msg.message
          })
          page = page + 1;


        }

      },
      fail: function (err) {
        console.log(err);
      }

    })
  },
  
//去支付
gotopay:function(e){
  var that = this;
  var orderid = e.currentTarget.dataset.id;
  console.log(orderid);
  var openid = app.userList.openid;
  var jumpurl = app.jumpurl.url;
  
  wx.redirectTo({
    url: "/pages/comfirmPay/comfirmPay?orderid="+orderid,
    
  })
},
  // wx.request({
  //   url: jumpurl + 'pay/index',
  //   header: {
  //     "Content-Type": "application/x-www-form-urlencoded"
  //   },
  //   method: "POST",
  //   data: {
  //     tag:0,
  //     openid: openid,
  //     orderid: orderid,
  //   },
  //   success: function (res) {
  //     console.log(res);
  //     console.log('-----------------------');

  //     if (res.data.state == 1) {
  //       that.setData({
  //         order_no: res.data.order_no,
  //       })
  //       // --------- 订单生成成功，发起支付请求 ------------------
  //       wx.requestPayment({
  //         timeStamp: res.data.timeStamp,
  //         nonceStr: res.data.nonceStr,   //字符串随机数
  //         package: res.data.package,
  //         signType: res.data.signType,
  //         paySign: res.data.paySign,
  //         'success': function (res) {
  //           //requestPayment:ok==>调用支付成功
  //           if (res.errMsg == "requestPayment:ok") {
  //             wx.request({
  //               url: jumpurl + 'pay/notify',
  //               header: {
  //                 "Content-Type": "application/x-www-form-urlencoded"
  //               },
  //               method: "POST",
  //               data: {
  //                 order_no: that.data.order_no,
  //               },
  //               success: function (res) {
  //                 wx.showToast({
  //                   title: '下单成功',
  //                   icon: 'none',
  //                   image: '',
  //                   duration: 2000,
  //                   mask: true,
  //                   success: function (res) {
  //                     setTimeout(function () {
  //                       that.onLoad();
  //                     }, 2000);
  //                   },
  //                 })
  //               }
  //             })
  //           }
  //         },
  //         'fail': function (res) {
  //           console.log(res);
  //         },
  //         'complete': function (res) {
  //           console.log(res);
  //         }
  //       })
  //     } else if (res.data.state == 0) {
  //       wx.showToast({
  //         title: res.data.Msg,
  //         icon: 'fail',
  //         duration: 1000
  //       })
  //     } else {
  //       wx.showToast({
  //         title: '系统繁忙',
  //         icon: 'fail',
  //         duration: 1000
  //       })
  //     }
  //   }
  // })



 // 打电话
  makePhoneCall:function(e){
    console.log(e.currentTarget.dataset.id)
    wx.request({
      url: jumpurl+'Telphone/index',
      data:{
        orderid:e.currentTarget.dataset.id
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //post
      },
      success:function(res){
        console.log(res)
        var phoneNumber;
        if (res.data.msg.message){
           phoneNumber = res.data.msg.message.tel;
          console.log("*******" + phoneNumber);
        }else{
          console.log("电话号码为空");
          phoneNumber = "0000"
         
        }
        wx.makePhoneCall({
          phoneNumber: phoneNumber, 
          success: function () {
            console.log("拨打电话成功！")
          },
          fail: function () {
            console.log("拨打电话失败！")
          }
        })
      },
      fail:function(err){
        console.log("获取电话号码失败"+err);
      }
    })
   
    
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  onLoad: function (options) {
    var that = this;
    tag = options.tips;
   this.setData({
      currentTab: parseInt(options.tips),
      noneData: false
      //isStatus: options.otype
    });
    wx.request({
      url: jumpurl+'dingdan/index',
      data:{
        tag: options.tips,
        page:1,
        openid:app.userList.openid
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //post
      },
      success:function(res){
        console.log(res);
        var flag = res.data.msg.SUCCESS;
        console.log("---------------"+flag+"--------------");
        if(flag=="F"){
          that.setData({
            isnone:true,
            noneData:false,
            acorres: res.data.msg.message
          })
        }else{
          var data_len = res.data.msg.message.length;
             acorres = res.data.msg.message
            that.setData({
              noneData: false,
              isnone:false,
              acorres: res.data.msg.message
            })
            page = page + 1;

         
        }
      
      },
      fail:function(err){
          console.log(err);
      }
       
      })
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        that.setData({
          winHeight: calc
        });
      }
    });
  },

 

 

  // 取消订单
  noTitlemodalTap: function (e) {
    var that = this;
    var jumpurl = app.jumpurl.url;
    wx.showModal({
      content: "您确定要取消订单吗？",
      confirmText: "确定",
      cancelText: "取消",
      success: function (res) {
        if (res.confirm) {
          console.log("确定取消，调用接口")
          wx.request({
            url: jumpurl + 'dingdan/delete',
            data: {
              orderid: e.currentTarget.dataset.id,
            },
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded" //post
            },
            success: function (res) {
              console.log("************************************")
              console.log(res);
              var acorres = that.data.acorres;
             
              var aco_len=acorres.length*1;
              console.log(aco_len);
              if(aco_len==1){
                console.log("*****************length<1************");
                that.setData({
                  isnone:true,
                  noneData:false,
                  acorres: res.data.msg.message
                })
              }else{
                for (var i = 0; i < acorres.length; i++) {
                  if (acorres[i].id == e.currentTarget.dataset.id) {
                    acorres.splice(i, 1)
                  }
                }
                that.setData({
                  isnone:false,
                
                  acorres: acorres,
                })
              }
              
              wx.showToast({
                title: '取消成功',
                duration:1000
              })
            },
            fail: function (err) {
              console.log("取消订单调用接口错误" + err)
            },
          })

        } else if (res.cancel) {
          console.log("用户取消");
        }
      }
    })
  },



  //触底加载更多

  LoadMore:function(){
    var that=this;
    console.log("loadmore");
    console.log(tag);
    wx.request({
      url: jumpurl + 'dingdan/index',
      data: {
        tag:tag,
        page:page,
        openid: app.userList.openid
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //post
      },
      success: function (res) {
       var data_len=res.data.msg.message.length;
        if(data_len<1){
          console.log("没有更多数据");
          that.setData({
            noneData:true,
            isnone:false
          })
        }else{
          wx.showLoading({
            title: '加载中……',
            duration: 1000,
          })
          for (var i = 0; i < res.data.msg.message.length; i++) {
            acorres.push(res.data.msg.message[i]);
          }
          setTimeout(() => {
            that.setData({
              // isHideLoadMore: true,
              acorres: acorres,
              noneData: false,
              isnone:false
            })
          }, 500)
          page = page + 1;
        }
      
        console.log("-----------------acorres-----------")
        console.log(page);
        console.log(acorres);
      },
      fail: function (err) {
        console.log(err);
      }

    })

   
    
   
   

  },
 
  footerTap: app.footerTap
})
